<?
//print_r($_REQUEST);
?>
<script type="text/javascript" charset="utf-8">
 $(function(){
 	$("select#id_categoria").change(function(){
 		$.getJSON("/aj_select.php?req=ordprod&id_negozio=<?echo $this->negozio->id;?>&",{id_categoria: $(this).val()}, function(j){
 			var options = '';
 			for (var i = 0; i < j.length; i++) {
 				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
 			}
 			$("#ordine").html(options);
 			$('#ordine option:first').attr('selected', 'selected');
 		})
 	})
 	
 	$(".cancella_immagine").click(function(){
 		var nf = $(this).attr('rel');
 		var n = $(this).attr('ord');
 		
 		if(confirm("Cancellare l'immagine dal disco?"))
 		{
	 		$.getJSON("/aj_upload.php?mod=del&id_negozio=<?echo $this->negozio->id;?>&id_prodotto=<?echo $prodotto->id;?>",{nome_file: $(this).attr('rel')}, function(j){
	 			var options = '';
	 			for (var i = 0; i < j.length; i++) {
	 				options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
	 			}
	 			
	 			 $('#riga_imm_'+n).html('Immagine cancellata!');
	 		})
	 	}
 	})
 	
    $('#uploadForm').ajaxForm({
        beforeSubmit: function(a,f,o) {
            o.dataType = $('#uploadResponseType')[0].value;
            $('#uploadStatus').html('Uploading...');
        },
        success: function(data) {
            var $out = $('#uploadOutput');
            
            //$out.append('Form success handler received: <strong>' + typeof data + '</strong>');
            if (typeof data == 'object' && data.nodeType)
                data = elementToString(data.documentElement, true);
            else if (typeof data == 'object')
                data = objToString(data);
            $out.append( data);
            $('#uploadStatus').html('Immagine caricata! Aggiungine altre.');
        }
    });
 })
 
 
 
 
</script>
<script>
function add_row(r, n_variante, id_variante, n_valore)
{
	var i=r.parentNode.parentNode.rowIndex; 
	var _row = document.getElementById('var_'+n_variante+'_table').insertRow(i);
	var cell0 = _row.insertCell(0);
	var cell1 = _row.insertCell(1);
	var cell2 = _row.insertCell(2);
	var cell3 = _row.insertCell(3);
	var cell4 = _row.insertCell(4);
	var cell5 = _row.insertCell(5);
	cell0.innerHTML = "<input name=\"var_"+n_variante+"_valore["+n_valore+"]\" size=\"5\" type=\"text\">";
	cell1.innerHTML = "<input name=\"var_"+n_variante+"_etichetta_ita["+n_valore+"]\" size=\"9\" type=\"text\">";
	cell2.innerHTML = "<input name=\"var_"+n_variante+"_etichetta_eng["+n_valore+"]\" size=\"9\" type=\"text\">";
	cell3.innerHTML = "<input name=\"img_var_"+n_variante+"_val["+n_valore+"]\" size=\"4\" type=\"file\" size=\"4\" >";
	cell4.innerHTML = "<input name=\"var_"+n_variante+"_default\" value=\""+n_valore+"\" type=\"radio\">";
	cell5.innerHTML = "<input src=\"/_admin/img/cancel.png\" onclick=\"return delete_row(this,'"+n_variante+"');\" type=\"image\">";
	return false;
}		
function delete_row(r, n_variante)
{
	if(confirm("Sicuro di voler cancellare questo valore?"))
	{
		var i=r.parentNode.parentNode.rowIndex; 
		document.getElementById('var_'+n_variante+'_table').deleteRow(i); 
	}
	return false;
}

function cancVar(ildiv)
{
	document.getElementById(ildiv).innerHTML = "<input type=\"file\" name=\"img_"+ildiv+"\" size=\"4\"/>";
}

function uncheck(radioObj, n_c)
{
	var radioLength = radioObj.length;
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked)
		{
			if(confirm("Disabilitare valore di default?")) radioObj[i].checked = false;
			else radioObj[i].checked = true;
		}else
		{
			radioObj[i].checked = false;
		}			
	}	
}
</script>
<!-- TinyMCE -->

<script type="text/javascript">
	tinyMCE.init({
		mode : "exact",
		elements : "elm1",
		theme : "simple"



	});
</script>
<!-- /TinyMCE -->



<div id="content">
  <h1 class="pagetitle"><? if($_REQUEST[mod]=="new") echo "Inserisci"; else echo "Modifica";?> Prodotto</h1>
  <a href="/admin/categoria.php?id_categoria=<? echo $prodotto->id_categoria;?>" id="rss-posts">Indietro</a>
  <div class="post">
    <h2 class="title"><? if($_REQUEST[mod]!="new") { echo $prodotto->nome_ita;}else{ echo "Nuovo prodotto"; }?></h2>
    <p class="byline"><small>
    		<? if($_REQUEST[mod]!="new") {?>
    		<a  href="/admin/categoria.php?id_categoria=<?echo $prodotto->id_categoria?>&mod="><img align="absmiddle" src="/_admin/img/folder_blue_open.png" border="0" /></a> <a target="negozio" href="/admin/categoria.php?id_categoria=<?echo $prodotto->id_categoria?>&mod=">Torna alla cartella</a> 
	      	&nbsp;&nbsp;    		
    		<a target="negozio" href="<? echo $prodotto->file; ?>.htm"><img align="absmiddle" src="/_admin/img/viewmag.png" border="0" /></a> <a target="negozio" href="<? echo $prodotto->file; ?>.htm">Anteprima</a>
	      	&nbsp;&nbsp;
	      	<a  href="prezzi.php?id_prodotto=<? echo $prodotto->id;?>&mod=mod"><img align="absmiddle" src="/_admin/img/euro.png" border="0" /></a> <a  href="prezzi.php?id_prodotto=<? echo $prodotto->id;?>&mod=mod">Prezzi e Quantità</a> 
	      	
    	 	<?}?>
    </small>
    </p>
    <div class="meta"></div>
    
    <div class="entry_sx"> Immagini </div>
      <form id="uploadForm" action="/aj_upload.php" method="post" enctype="multipart/form-data">
      <div class="entry_dx"> 
                <input name="MAX_FILE_SIZE" value="1000000" type="hidden">
                <input name="immagini_0" type="file">
                <input type="hidden" id="id_prodotto" name="id_prodotto" value="<? echo $prodotto->id; ?>" /> 
                <input type="hidden" id="id_negozio" name="id_negozio" value="<? echo $prodotto->id_negozio; ?>" />
                <input type="hidden" id="uploadResponseType" name="mimetype" value="html" /> 
                <!-- select id="uploadResponseType" name="mimetype">
                    <option value="html">html</option>
                    <option value="json">json</option>
                    <option value="script">script</option>
                </select-->
                <input value="Submit" type="submit" />
            

           
                 <div id="uploadStatus"></div>      
           
            <div id="uploadOutput"></div>
            <div id="uploadOld">
                       
            <?
       
		if(is_array($prodotto->get_immagini()))foreach($prodotto->get_immagini() as $n=>$nome_file)
		{
			echo "<div id=\"riga_imm_".$n."\" >";
			echo ' <table> <tr ><td><p><img width="30px" align="left" src="/img/S/' . $nome_file . '">' ; //
			echo '  ' . $nome_file.'</p></td><td><a class="cancella_immagine" rel="'.$nome_file.'" ord="'.$n.'" href="javascript:;" >X</a></td></tr></table>' ; //
			echo "</div>";
		}
            ?>
            </div></div>
      </form>
      
    <form id="form1" name="form1" action="" method="POST" enctype="multipart/form-data">
      <div class="meta"></div>
      <div class="entry_sx"> Nome </div>
      <div class="entry_dx">
        <input name="nome_ita" type="text" value="<? echo $prodotto->nome_ita;?>" size="50" />
        ITA<br/>
        <input name="nome_eng" type="text" value="<? echo $prodotto->nome_eng;?>" size="50" />
        ENG </div>
      <div class="meta"></div>
      <div class="entry_sx"> Nome file </div>
      <div class="entry_dx">
        <input name="file_ita" type="text" id="file_ita" value="<? echo $prodotto->file_ita;?>" size="50" />
        ITA<br/>
        <input name="file_eng" type="text" id="file_eng" value="<? echo $prodotto->file_eng;?>" size="50" />
        ENG </div>
      <div class="meta"></div>
      
      
      
      <div class="entry_sx"> Descrizione </div>
      <div class="entry_dx">
        <textarea name="descrizione_ita" cols="48" rows="3"><? echo $prodotto->descrizione_ita;?></textarea>
        ITA<br/>
        <textarea name="descrizione_eng" cols="48" rows="3"><? echo $prodotto->descrizione_eng;?></textarea>
        ENG </div>
      <div class="meta"></div>
      <div class="entry_sx"> Cartella </div>
      <div class="entry_dx">
        <select name="id_categoria" id="id_categoria">
          <?
		foreach($this->negozio->cat_struct[categorie] as $id_cat=>$tmp_cat)
		{
			$indent = ""; for($i=0;$i<=$tmp_cat[liv];$i++)$indent .= "&nbsp;&nbsp;&nbsp;"; $indent.="|-";
			$sel = ""; if($prodotto->id_categoria==$id_cat) $sel = "selected";
			echo "<option value=\"$id_cat\" $sel >$indent $tmp_cat[nome] $tmp_cat[percorso]/</option>";
		}
		?>
        </select>
      </div>
      <div class="meta"></div>
      <div class="entry_sx"> Ordine </div>
      <div class="entry_dx">
        <select name="ordine" id="ordine">
          <?
		echo $prodotto->get_ordine_options();

		?>
        </select>
      </div>
      <script>
	  
      function show_div()
	  {
		  if( document.getElementById("id_variante_1").value <= 0 )
		  	document.getElementById("var_1_div").style.visibility = "hidden";
		  else 
		  	document.getElementById("var_1_div").style.visibility = "visible";
		  
		  if( document.getElementById("id_variante_1").value <= 0 ) 
		  	document.getElementById("var_1_div").style.display = "none";
		  else 
		  	document.getElementById("var_1_div").style.display = "block";
		  
		  if( document.getElementById("id_variante_2").value <= 0 )
		   document.getElementById("var_2_div").style.visibility = "hidden";
		  else 
		  	document.getElementById("var_2_div").style.visibility = "visible";
			
		  if( document.getElementById("id_variante_2").value <= 0 ) 
		  	document.getElementById("var_2_div").style.display = "none";
		  else 
		  	document.getElementById("var_2_div").style.display = "block";
		}
      </script>
      
      <?
      for($i=1;$i<=2;$i++)
      {
      ?>
      <div class="meta"></div>
      <div class="entry_sx"> Variante <?echo $i;?> </div>
      <div class="entry_dx">
        <select name="id_variante_<?echo $i;?>" id="id_variante_<?echo $i;?>" onchange="show_div();">
        	
          <?
		foreach($this->negozio->get_varianti() as $idv=>$variante)
		{
			$sel = ""; if($prodotto->{'id_variante_'.$i}==$idv) $sel = "selected";
			if($idv>0) echo "<option value=\"$idv\" $sel >$variante[nome]</option>";
			else echo "<option value=\"0\" $sel >Nessuna variante</option>";
		}

		?>
        </select>
        <div id="var_<? echo $i; ?>_div">
        <table id="var_<? echo $i; ?>_table" width="100%" border="0" cellspacing="3" cellpadding="3">
		  <tr>
		    <td>Valore</td>
		    <td>Etichetta ITA</td>
		    <td>Etichetta ENG</td>
		     <td></td>
		    <td>Default</td>
		    <td></td>
		  </tr>
		  <?
		  	$n = 0;
		  	if(is_array($prodotto->varianti[$prodotto->{'id_variante_'.$i}][valori]))
		  	{
		  		foreach($prodotto->varianti[$prodotto->{'id_variante_'.$i}][valori] as $valore=>$etichetta)
				{
			  ?>
			  <tr>
			    <td><input type="text" name="var_<? echo $i?>_valore[<?echo $n;?>]" size="5" value="<? echo $valore?>"/></td>
			    <td><input type="text" name="var_<? echo $i?>_etichetta_ita[<?echo $n;?>]" value="<? echo $etichetta[ita]?>" size="9"/></td>
			    <td><input type="text" name="var_<? echo $i?>_etichetta_eng[<?echo $n;?>]" value="<? echo $etichetta[eng]?>" size="9"/></td>
			    <td>
			    	
			     <? $nome_file = $prodotto->varianti[$prodotto->{"id_variante_$i"}][nome_file][$valore]; 
			    if(strlen($nome_file)>0)
			    {
			    	echo "<div id=\"var_".$i."_val_".$n."\"><img height=\"20px\" src=\"/var/S/$nome_file\" /> <a href=\"javascript:cancVar('var_".$i."_val_".$n."')\">mod</a> <input type=\"hidden\" name=\"img_var_".$i."_val_".$n."\" value=\"$nome_file\"/></div>";
			    }else
			    {
			    	?>
			    	<input type="file" name="img_var_<? echo $i?>_val_<?echo $n;?>" size="4"/>
			    	<?
			    }
			    ?>
			    </td>
			    <td><input name="var_<? echo $i?>_default" type="radio" value="<?echo $n;?>" <?if($valore==$prodotto->varianti[$prodotto->{"id_variante_$i"}][selected]) echo "checked"; ?> /></td>
			    <td><input type="image" src="/_admin/img/cancel.png" onclick="return delete_row(this,'<?echo $i;?>');" /></td>
			  </tr>
			  <?
			  		$n++;
				}
		  	}else 
		  	{
		  		?>
		  	  <tr>
			    <td colspan="6" align="center"><small> Nessun valore inserito per questa variante.</small></td>			    
			  </tr>	
			  <?
		  	}
		  ?>
		  	<tr>
			    <td colspan="6" ><input type="image" align="absmiddle" src="/_admin/img/edit_add.png"  onclick="return add_row(this,'<?echo $i;?>', '<? echo $prodotto->{"id_variante_$i"}?>', '<? echo $n; ?>');" /> Aggiungi nuovo valore</td>			    
			  </tr>
		</table>
      </div>
      </div>
      <?
      }
	  
	  $anagrafica = $prodotto->getAnagrafica();
	  $i=0;
	  if(count($anagrafica)>0)foreach($anagrafica as $ida=>$campo)
	  {
		  $i++;
		  ?>
            <div class="meta"></div>
              <div class="entry_sx"> <? echo $campo[etichetta];?> (<? echo $campo[nome];?>) </div>
              <div class="entry_dx">
   			<? 
			if($campo[tipo] == "HTML")
			{
			?>
	
    			<textarea id="elm1" name="anagr[<? echo $campo[nome];?>]" rows="15" cols="80" style="width: 100%"><? echo $campo[valore];?></textarea>
    			
           


			<?	
			}
			else
			{
			?>
			<textarea name="anagr[<? echo $campo[nome];?>]" rows="5" cols="70" ><? echo $campo[valore];?></textarea>
         
			<?	
			}
			?>
              </div>	  
		  
		  <?
		}
      ?>
     
      
      <script>
      show_div();
      </script>
      <div class="meta">
        <input name="mod" value="save" type="hidden" />
        <input name="id_prodotto" value="<? echo $prodotto->id; ?>" type="hidden" />
        <input type="reset" name="Annulla" value="Annulla" class="pulsanti"/> <input type="submit" name="Salva" value="Salva" class="pulsanti"/></div>
    </form>
  </div>
</div>
